package com.nl.controller.web.service;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.MgrState;
import com.nl.controller.web.bean.MgrType;
import com.nl.controller.web.bean.SmsMgrView;
import com.nl.controller.web.mapper.SmsMgrViewMapper;
import com.nl.controller.web.mapper.UtilMapper;

import lombok.extern.slf4j.Slf4j;


@Service
@Slf4j
public class SmsMgrService {
    @Autowired
    private SmsMgrViewMapper smsMgrViewMapper;

    @Autowired
    private CommonService commonService;
    
    @Autowired
    private UtilMapper utilMapper;

    @SystemServiceLog(description = "短信项目视图", value = false)
    
    public PageInfo<SmsMgrView> findregionView( String flag,String cityId) {
    	PageInfo<SmsMgrView> pageInfo;
        try {
            List<SmsMgrView> list = this.getDateList(flag, cityId);

            pageInfo = new PageInfo<>(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信项目视图查询出现异常" );
            throw new SmsException("短信项目视图查询出现异常" ); 
        }
        return pageInfo;
    }
    
    @SystemServiceLog(value = false, description = "数据源获取")
  public List<SmsMgrView> getDateList(String flag, String cityId) {
      List<SmsMgrView> list = new ArrayList<>();
      SmsMgrView smsMgrView1=new SmsMgrView();
      int runNumAll=0;
      int waitNumAll=0;
      int stopNumAll=0;
      //按地市
      if ("city".equals(flag)) {
          List<SmsMgrView> cityList = new ArrayList();
          try {
              cityList = smsMgrViewMapper.stateCount();
              log.info("获取地市列表");
          } catch (Exception e) {
              e.printStackTrace();
              log.info("获取地市列表异常");        
          } 
          for(SmsMgrView smsMgrView:cityList){
        	  String runNum= smsMgrView.getRunNum();
        	  if(runNum==null)
              {
        		  runNum ="0";                        
              }
        	  runNumAll = runNumAll +Integer.parseInt(runNum);
        	  String waitNum= smsMgrView.getWaitNum();
        	  if(waitNum==null)
              {
        		  waitNum ="0";                        
              }
        	  waitNumAll = waitNumAll +Integer.parseInt(waitNum);
        	  String stopNum= smsMgrView.getStopNum();
        	  if(stopNum==null)
              {
        		  stopNum ="0";                        
              }
        	  stopNumAll = stopNumAll +Integer.parseInt(stopNum);
          } 
          //smsMgrView1.setCityName("合计");
          smsMgrView1.setRunNum(String.valueOf(runNumAll));
          smsMgrView1.setWaitNum(String.valueOf(waitNumAll));
          smsMgrView1.setStopNum(String.valueOf(stopNumAll));
          cityList.add(smsMgrView1);
          return cityList;
          }else if ("county".equals(flag)) {
          List<SmsMgrView> countyList = new ArrayList();
          Map<String, Object> countyMap = new HashMap();
          countyMap.put("cityId",cityId);
          try {
          	countyList = smsMgrViewMapper.stateCountCounty(cityId);
              log.info("获取区县列表");
          } catch (Exception e) {
              e.printStackTrace();
              log.info("获取区县列表异常");
          }
          for (SmsMgrView a : countyList) {
              try {
            	  if(("".equals(a.getCountyId()))||(a.getCountyId()==null)){
            		  a.setCountyName("市级");
            	  }else{
            	  a.setCountyName(smsMgrViewMapper.findCountyName(a.getCountyId()));
            	  }
                  log.info("按区县依次获取数据源" + a.getCountyId()+ a.getCountyName());
              } catch (Exception e) {
                  e.printStackTrace();
                  log.info("按区县依次获取数据源异常" + a.getCountyId());
              } 
              list.add(a);
          }
          for(SmsMgrView smsMgrView:countyList){
        	  String runNum= smsMgrView.getRunNum();
        	  if(runNum==null)
              {
        		  runNum ="0";                        
              }
        	  runNumAll = runNumAll +Integer.parseInt(runNum);
        	  String waitNum= smsMgrView.getWaitNum();
        	  if(waitNum==null)
              {
        		  waitNum ="0";                        
              }
        	  waitNumAll = waitNumAll +Integer.parseInt(waitNum);
        	  String stopNum= smsMgrView.getStopNum();
        	  if(stopNum==null)
              {
        		  stopNum ="0";                        
              }
        	  stopNumAll = stopNumAll +Integer.parseInt(stopNum);
          } 
          smsMgrView1.setCountyName("合计");
          smsMgrView1.setRunNum(String.valueOf(runNumAll));
          smsMgrView1.setWaitNum(String.valueOf(waitNumAll));
          smsMgrView1.setStopNum(String.valueOf(stopNumAll));
          countyList.add(smsMgrView1);
          return countyList;
      }
      return list;
  }
      
    
    @SystemServiceLog(description = "短信项目视图", value = false)
    public List<SmsMgrView> findMgrView() {
        List<SmsMgrView> list=null;
        try {
            list = smsMgrViewMapper.findMgrView();

        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信项目视图查询出现异常" );
            throw new SmsException("短信项目视图查询出现异常" );
        }
        return list;
    }


    @SystemServiceLog(description = "短信项目视图状态查询", value = false)
    public List<MgrState> stateCount() {
        List<MgrState> list=null;
        try {
            list = smsMgrViewMapper.stateCount1();
            //获取所有地市项目状态的汇总
        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信项目视图地市状态查询出现异常" );
            throw new SmsException("短信项目视图地市状态查询出现异常" );
        }
        return list;
    }
    @SystemServiceLog(description = "短信项目视图状态汇总", value = false)
    public List<MgrState> stateCountAll() {
        List<MgrState> list=null;
        try {
            list = smsMgrViewMapper.stateCountAll();
            //获取所有地市项目状态的汇总
        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信项目视图地市状态查询出现异常" );
            throw new SmsException("短信项目视图地市状态查询出现异常" );
        }
        return list;
    }

    @SystemServiceLog(description = "导出", value = false)
    public JSONArray download
            (String flag, String cityId) {
        {
            List<SmsMgrView> list = new ArrayList<>();
            
            try {
                list = this.getDateList(flag,cityId);
                commonService.createSmsOperLog("7", "0","导出项目视图信息", "0", "25");
            } catch (Exception e) {
                commonService.createSmsOperLog("7", "0","导出项目视图信息", "1", "25");
                throw new SmsException("项目视图导出失败");
            }
            JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(list));
            return jsonArray;
        }
    }
    
    @SystemServiceLog(description = "查询项目类型" , value = false)
    public List<MgrType> findMgrType(){
    	List<MgrType> list=new ArrayList<MgrType>();
    	try {
    		list = utilMapper.findMgrType() ;
		} catch (Exception e) {
			log.error("----find a exception :"+e.toString());
			e.printStackTrace();
		}
    	return list;
    }
    
    @SystemServiceLog(description = "查询项目类型" , value = false)
    public String findMgrTypeById(String typeId){
    	String result="";
    	try {
       	 result=utilMapper.findMgrTypeById(typeId);

		} catch (Exception e) {
			log.error("----find a exception :"+ e.toString());
			e.toString();
		}
    	return result;
    }
}
